<?php

namespace app\api\model\hotel;

use app\admin\model\hotel\HotelRoomType;
use app\admin\model\User;
use app\admin\model\user\UserLevel;
use think\Model;


class HotelRoom extends Model
{

    

    

    // 表名
    protected $name = 'hotel_room';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'label_text'
    ];



    public function getLabelTextAttr($value,$data)
    {

        if (!empty($data['label'])) {
            return (array)json_decode($data['label'], true);
        }
        return [];
    }

    public function getRoomPrice($room_type_id,$user_id)
    {
        $data = [
            'member_price'=>0,
            'price'=>0
        ];
        //$room = HotelRoom::get($room_id);
        $room_type = HotelRoomType::get($room_type_id);
        $data['price'] = $room_type['price'];
        $data['member_price'] = $room_type['price'];
        $user = User::get($user_id);

        $user_level = UserLevel::where(['level_id'=>$user->level])->find();
        if ($user_level){
            $discount = $user_level['discount'];

            $data[ 'member_price' ] = number_format($room_type[ 'price' ] *$discount / 100, 2, '.', '');

        }

        return $data;
    }


    public function getRoomDiscountPrice($room_type_id)
    {
        $data = [
            'member_price'=>0,
            'price'=>0
        ];
        //$room = HotelRoom::get($room_id);
        $room_type = HotelRoomType::get($room_type_id);

        $user_level = UserLevel::where(['level_id'=>2])->find();

        $discount = $user_level['discount'];

        $data[ 'member_price' ] = number_format($room_type[ 'price' ] * $discount / 100, 2, '.', '');

        return $data;
    }

}
